/**
 * 
 */
package com.zc.BankCreditManagement.DAO;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.zc.BankCreditManagement.BasicDAO.BasicDAO;
import com.zc.BankCreditManagement.JavaBean.Manager;

/**
 * @author ZhangCheng
 * @time 2019年9月17日
 */
public class ManagerDAO extends BasicDAO<Manager> {
	/**
	 * ManagerDAO无参构造 重载父类构造器
	 */
	public ManagerDAO() {
		super();
	}

	/**
	 * ManagerDAO有参构造 重载父类构造器
	 */
	public ManagerDAO(String url, String user, String pwd) {
		super(url, user, pwd);
	}

	/**
	 * 新增新客户，并插入到数据库中，参数是Manager对象
	 */
	public int insertNewMan(Manager man) {
		int count = 0;
		String sql = "Insert into customer values(?,?,?,?,?,?)";
		try {
			count = super.updateDML(sql, man.getManId(),  man.getManName(), man.getManLevel(), man.getManWorkPhone(), man.getEmail(), 0);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return count;
	}

	/**
	 * @apiNote 根据经理id删除经理 当数据库内不存在该经理时 返回 0 此时代表已被删除或者根本不存在 若要判断是其中哪一种状态
	 *          请使用get方法并设置excludeDelete为false
	 */
	public int deleteManById(int manId) {
		int count = 0;
		String sql = "update manager set isDelete = 1 where manid = ?";
		try {
			count = super.updateDML(sql, manId);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return count;
	}

	/**
	 * getManagerInfoById(String manId,boolean excludeDelete) 通过经理Id获取该经理所有信息
	 */
	public Manager getManagerInfoById(int manId, boolean excludeDelete) {
		StringBuffer sql = new StringBuffer("select * from manager where manid = ? ");
		if (excludeDelete)
			sql.append("and isdelete = 0");// 如果排除被删除的，那么只返回未被删除的
		ArrayList<Manager> m = getInfoFromDB(sql.toString(), manId);
		if (m.size() == 0) {
			return null;
		} else {
			return m.get(0);
		}

	}

	/**
	 * updateManInfo(Manager man) 根据客户对象所修改的数据来更新数据
	 */
	public int updateManInfo(Manager man,boolean excludeDelete) {
		int count = 0;
		boolean haveFirst = false;
		StringBuffer sql = new StringBuffer("update manager set ");
		Manager manDB = getManagerInfoById(man.getManId(), excludeDelete);

		if (!man.getManName().equals(manDB.getManName())) {
			sql.append("manname = '" + man.getManName() + "' ");
		}
	
		if (!(man.getManLevel() == manDB.getManLevel())) {
			if(haveFirst) sql.append(",");
			haveFirst = true;
			sql.append("ManLevel = '" + man.getManLevel() + "' ");
		}
		if (!man.getManWorkPhone().equals(manDB.getManWorkPhone())) {
			if(haveFirst) sql.append(",");
			haveFirst = true;
			sql.append("ManWorkPhone = '" + man.getManWorkPhone() + "' ");
		}

		sql.append("where manid = ? ");
		try {
			count = updateDML(sql.toString(), man.getManId());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return count;

	}

	/**
	 * getManagersBySQLFromDB(String sql,Object...obj)
	 * 通过自行传入sql语句以及可变参数获取该sql语句需要进行操作的变量 来进行查询
	 * 
	 * 返回一个Manager类的集合
	 * 
	 * @apiNote 注意：只能传入SELECT * 的语句，否则会报参数不匹配异常
	 */
	public ArrayList<Manager> getManagersBySQLFromDB(String sql, Object... obj) {
		return super.getInfoFromDB(sql, obj);
	}
	
	public ArrayList<Manager> getManagersInfo(boolean excludeDelete){
		StringBuffer sql = new StringBuffer("select * from manager ");
		if (excludeDelete)
			sql.append("where isdelete = 0");// 如果排除被删除的，那么只返回未被删除的
		System.out.println(sql);
		return super.getInfoFromDB(sql.toString());
	}

	@Override
	public ArrayList<Manager> resultSetToArrayList(ResultSet re) {
		ArrayList<Manager> m = new ArrayList<Manager>();

		try {
			while (re.next()) {
				m.add(new Manager(re.getInt("manid"), re.getString("manname"), re.getInt("manlevel"),
						re.getString("manworkphone"),re.getString("email")));
//				System.out.println(c.get(0).getInfo());
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		super.closeAll();
		return m;
	}

}
